package com.gxc.blogm.service;

import java.util.List;
import java.util.Map;

import com.gxc.blogm.domain.Frendlink;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.ContextUtil;

public class FrendlinkService {

	SqlDao sqlDao;

	public SqlDao getSqlDao() {
		return sqlDao;
	}

	public void setSqlDao(SqlDao sqlDao) {
		this.sqlDao = sqlDao;
	}

	/**
	 * 获取单个文件
	 * 
	 * @param id
	 * @return
	 */
	public Frendlink retrieve(String id) {
		return sqlDao.selectOne("blog.frendlink.retrieve", id);
	}

	/**
	 * 创建新文件
	 * 
	 * @param frendlink
	 * @return
	 */
	public int create(Frendlink frendlink) {
		int i = sqlDao.insert("blog.frendlink.create", frendlink);
		this.updateCache();
		return i;
	}

	/**
	 * 更新文件信息
	 * 
	 * @param frendlink
	 * @return
	 */
	public int update(Frendlink frendlink) {
		int i = sqlDao.update("blog.frendlink.update", frendlink);
		this.updateCache();
		return i;
	}

	/**
	 * 删除单个文件
	 * 
	 * @param id
	 * @return
	 */
	public int delete(String id) {
		int i = sqlDao.delete("blog.frendlink.delete", id);
		this.updateCache();
		return i;
	}

	/**
	 * 批量删除文件
	 * 
	 * @param id_array
	 * @return
	 */
	public int delete(String[] id_array) {
		int i = 0;
		for (String id : id_array) {
			i += sqlDao.delete("blog.frendlink.delete", id);
		}
		this.updateCache();
		return i;
	}

	/**
	 * 列出所有文件
	 * 
	 * @param parameter
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List listAll(Map parameter) {
		return sqlDao.selectList("blog.frendlink.list", parameter);
	}

	/**
	 * 分页列出文件
	 * 
	 * @param parameter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageList listPage(Map parameter, PageInfo pageInfo) {
		return sqlDao
				.selectPageList("blog.frendlink.list", parameter, pageInfo);
	}

	@SuppressWarnings("rawtypes")
	public List retrieveLatest() {
		return sqlDao.selectList("blog.frendlink.retrieveLatest");
	}

	@SuppressWarnings({ "unused", "rawtypes" })
	private void updateCache() {
		List list = this.retrieveLatest();
		CacheService cacheService = (CacheService) ContextUtil
				.getBean("blogCacheService");
		cacheService.reloadFrendlinkList();
	}
}
